package cn.iocoder.yudao.module.game.service.addMoney;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.addMoney.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.addMoney.AddMoneyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.addMoney.AddMoneyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 加钱 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AddMoneyServiceImpl implements AddMoneyService {

    @Resource
    private AddMoneyMapper addMoneyMapper;

    @Override
    public Long createAddMoney(AddMoneySaveReqVO createReqVO) {
        // 插入
        AddMoneyDO addMoney = BeanUtils.toBean(createReqVO, AddMoneyDO.class);
        addMoneyMapper.insert(addMoney);
        // 返回
        return addMoney.getId();
    }

    @Override
    public void updateAddMoney(AddMoneySaveReqVO updateReqVO) {
        // 校验存在
        validateAddMoneyExists(updateReqVO.getId());
        // 更新
        AddMoneyDO updateObj = BeanUtils.toBean(updateReqVO, AddMoneyDO.class);
        addMoneyMapper.updateById(updateObj);
    }

    @Override
    public void deleteAddMoney(Long id) {
        // 校验存在
        validateAddMoneyExists(id);
        // 删除
        addMoneyMapper.deleteById(id);
    }

    private void validateAddMoneyExists(Long id) {
        if (addMoneyMapper.selectById(id) == null) {
            throw exception(ADD_MONEY_NOT_EXISTS);
        }
    }

    @Override
    public AddMoneyDO getAddMoney(Long id) {
        return addMoneyMapper.selectById(id);
    }

    @Override
    public PageResult<AddMoneyDO> getAddMoneyPage(AddMoneyPageReqVO pageReqVO) {
        return addMoneyMapper.selectPage(pageReqVO);
    }

}